<?php

namespace app\adminapi\lists\room\department;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExcelInterface;
use app\common\model\room\department\RoomDepartmentDevice;
use app\common\lists\ListsSearchInterface;


/**
 * 科室设备关联列表
 * Class RoomDepartmentDeviceLists
 * @package app\adminapi\listsroom
 */
class RoomDepartmentDeviceLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface {

    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function setSearch(): array {
        return [
            '=' => ['status'],

        ];
    }


    /**
     * @notes 搜索条件
     * @
     * @date 2023/2/24 16:08
     */
    public function queryWhere() {
        $where = [];

        if (!empty($this->adminCid) && $this->adminCid !== 0) {
            $where[] = ['cid', '=', $this->adminCid];
        }

        if (!empty($this->params['device'])) {
            if (isset($this->params['device'][0])) {
                $where[] = ['cid', '=', $this->params['device'][0]];
            }
            if (isset($this->params['device'][1])) {
                $where[] = ['device_id', '=', $this->params['device'][2]];
            }
        }
        if (!empty($this->params['department'])) {
            if (isset($this->params['department'][0])) {
                $where[] = ['cid', '=', $this->params['department'][0]];
            }
            if (isset($this->params['department'][1])) {
                $where[] = ['bid', '=', $this->params['department'][1]];
            }
            if (isset($this->params['department'][2])) {
                $where[] = ['rid', '=', $this->params['department'][2]];
            }
        }

        if (!empty($this->params['create_time_start']) && !empty($this->params['create_time_end'])) {
            $where[] = ['log_time', 'between time', [$this->params['create_time_start'], $this->params['create_time_end']]];
        }

        return $where;
    }

    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function lists(): array {
        $list = RoomDepartmentDevice::where($this->searchWhere)
//            ->field('*')
            ->with([
//                'deviceItem',
                'campus', 'building', 'room', 'device',
            ])->where($this->queryWhere())
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['sort' => 'asc', 'rid' => 'asc', 'id' => 'asc'])
            ->select()
            ->toArray();
        foreach ($list as $k => $item) {
            $other_money = bcadd($item['lift_fee'], $item['second_pressure_fee'], 6);
            $list[$k]['total_money'] = bcadd($item['total_fee'], $other_money, 6);
//            $list[$k]['device_type'] = $item['deviceItem']['device_type'];
        }

        return $list;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function count(): int {
        return RoomDepartmentDevice::where($this->searchWhere)->where($this->queryWhere())->count();
    }


    /**
     * @notes 导出文件名
     * @return string
     * @author 段誉
     * @date 2022/11/24 16:17
     */
    public function setFileName(): string {
        return '科室报表清单';
    }

    public function setExcelLists() {
        return null;
    }

    /**
     * @notes 导出字段
     * @return string[]
     * @author 段誉
     * @date 2022/11/24 16:17
     */
    public function setExcelFields(): array {
        return [
            'device_id' => '设备编号',
            'devices' => '设备信息',
            'departments' => '科室信息',
            'device_type' => '设备类型',
            'base_number' => '底数',
            'reading' => '读数',
            'use_total' => '用量',
            'coefficient' => '系数',
            'price' => '价格',
            'total_fee' => '水电费',
            'lift_fee' => '电梯费',
            'second_pressure_fee' => '二次加压费',
            'total_money' => '总费用',
            'log_time' => '最后记录时间',
        ];
    }
}
